<template>
  <div class="i_keshi">
    <!--搜索-->
    <div class="i-keshi_main">
      <div class="ibanner_header">
        <Button type="info" @click="navto">
          <i class = "iconfont icon-add" style="font-size:14px;margin-right:5px"></i>
        </Button>
        <div class="box">
          <div class="header_input">
            <span></span>
            <Input
              v-model.trim="val"
              placeholder="请输入一级科室或二级科室查询"
              style="width: 200px;margin-right:10px;"
              clearable
            />
          </div>
          <Button type="primary" icon="ios-search" @click="change"></Button>
        </div>
      </div>
      <!--列表-->
      <div class="tabList">
        <Table stripe :columns="columnList" :data="tableList"></Table>
      </div>
    </div>
    <div class="page">
      <Page :total="medicineSize" :current='pageNo' show-total show-elevator @on-change="pageChange"/>
    </div>
  </div>
</template>

<script>
// 院内科室
import tmpHeader from "@/pages/organizat/contentmen/tmpHeader";
import api from "@/api/commonApi";
import cookie from "@/utils/cookie.js";
import specialized from "../../../api/specialized";
export default {
  components: {
    tmpHeader
  },
  data() {
    return {
      tableList: [],
      id: sessionStorage.getItem("hospitalId"),
      medicineSize: 10,
      val: "",
      pageNo: 1,
      pageSize: 10,
      columnList:[
        {
          title:"排序",
          key:"sum",
          align:"center",
          render: (h, params) => {
            let sum = (this.pageNo-1)*this.pageSize + params.index +1;
            return h("span", sum);
          }
        },
        {
          title:"一级科室",
          key:"parentDictName",
          align:"center",
        },
        {
          title:"二级科室",
          key:"dictName",
          align:"center",
        },
        {
          title:"院内名称",
          key:"deptNickname",
          align:"center",
        },
        {
          title:"科室图标",
          key:"departmenticon",
          align:"center",
          render: (h, params) => {
            let avatar = this.pictureFormat(params.row.icon);
            if (Boolean(avatar)) {
              avatar = this.fileBaseUrl + avatar;
              return h("img", {
                attrs: {
                  src: avatar || "",
                  style: "width:40px;height:40px;"
                }
              })
            } else {
              return h("span", {
                attrs:{
                  style:"color:gray;"
                }
              },'暂无图标')
            }
          }
        },
        {
          title:"特色科室",
          key:"specialDept",
          align:"center",
        },
        {
          title:"预约科室",
          key:"registeredReservation",
          align:"center",
        },
        {
          title:"对接号源池",
          key:"thirdPartyRegistration",
          align:"center",
          width: 140
        },
        {
          title:"显示排序",
          key:"showindex",
          align:"center",
        },
        {
          title:"设置",
          align:"center",
          render: (h,params) => {
            let row = params.row;
            return [
              h('span',{
                style: {
                  color: " #5362e1",
                  width: "60px"
                },
                on: {
                  click: () => {
                    this.edit(row)
                  }
                }
              },'编辑'),
              ' | ',
              h('span',{
                style: {
                  color: "#FEA443",
                  width: "60px"
                },
                on: {
                  click: () => {
                    this.updateCache(row)
                  }
                }
              },'更新缓存')
            ]
          }
        }
      ]
    };
  },
  created() {
    let breadList = [
      { path: "/index", title: "首页" },
      {
        path: "/index/operation/mechanism/index",
        title: "机构运营"
      },
      {
        path: "/index/operation/home",
        title: "机构配置管理"
      }
    ];
    this.$emit("changeBreadList", breadList);

  },
  mounted() {
    let pageNo = this.$route.params.pageNo;
    if (pageNo) {
      this.pageNo = parseInt(pageNo);
    }
    this.getMedicineData(this.pageNo);
  },
  methods: {
    //分页器改变
    pageChange(index) {
      this.pageNo = index;
      if (this.val) {
        this.getMedicineData(index, this.val);
      } else {
        this.getMedicineData(index);
      }
    },
    // 模糊查询
    change() {
      this.pageNo = 1
      this.getMedicineData(1, this.val);
    },
    navto() {
      this.functionJS.paramsNavgationTo(this, "departmentAdd", {
        // 公用方法
        pageNo: this.pageNo
      });
    },
    edit(item) {
      let id = item.id;
      let parentDept = item.parentDept;
      this.functionJS.paramsNavgationTo(this, "departmentEdit", {
        // 公用方法
        id,
        parentDept,
        pageNo: this.pageNo
      });
    },
    getMedicineData(pageNo, val) {
      //获取父级科室
      let department = cookie.getCookie("department") || '';
      //获取医院id
      let hospitalId = cookie.getCookie("JGhospitalId") || '';
      let params = {
        hospitalId,
        department,
        pageNo,
        pageSize: 10
      };
      // if (Boolean(val)) {
      //   params.searchKey = val.trim();
      // }
      this.$axios
        .post(api.userDepartmentList, params)
        .then(res => {
          if (res.data.success) {
            let ret = res.data.object;
            console.log(ret);
            this.medicineSize = ret.total;
            if(ret.records && ret.records.length){
              ret.records.forEach((item,index) => {
                item.registeredReservation = item.registeredReservation == '1'? '开启' : "关闭"
                item.specialDept = item.specialDept == '1'? '开启' : "关闭"
                item.thirdPartyRegistration = Number(item.thirdPartyRegistration) == 1 ? '开启' : '关闭'
                item.sum = this.addZeros(index)
              })
              this.tableList = ret.records;
            } else {
              this.tableList = []
            }
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //更新缓存
    updateCache(item){
      let params = {};
      params.id = item.id;
      console.log(item,'==========item')
      this.$axios
        .post(api.updateDepartmentCache, params)
        .then(resp => {
          if (resp.data.success) {
            this.$Message.info("更新成功");
          } else {
          }
        })
        .catch(err => {
          // this.$Message.info("服务器超时，请重试");
        });
    }
  }
};
</script>

<style scoped lang="less">
.i_keshi {
  width: calc(100%);
  padding: 10px 30px;
  margin: 0 auto;
  background: #fff;
  .i-keshi_main {
    width: 95%;
    margin: 10px auto;
    .ibanner_header {
      width: 100%;
      margin: 0 auto;
      display: flex;
      align-items: center;
      .box {
        margin-left: 10px;
        display: flex;
        flex-direction: row;
      }
    }
    .tabList {
      width: 100%;
      margin: 20px 0;
    }
  }
  .page{
    text-align: center;
  }
}
</style>
